﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DALFactory;
using KPIS.DBM;

namespace KPIS.GERP.GHRMS.BLL
{
    public class RecruitNewEmployeeBLL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public DataTable SelectTmpEducation(int tmp_emp_seq)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@tmp_emp_seq", tmp_emp_seq);

                string strSQL;

                strSQL = "SELECT edu.*"
                        + ", d.EDU_DEGREE_NAME, q.EDU_QUAL_NAME, m.EDU_MAJOR_NAME, i.EDU_INS_NAME"
                    + " FROM HRM_TMP_EMPLOYEE_EDUCATIONS edu"
                        + " LEFT JOIN MAS_EDU_QUALIFICATIONS q ON edu.EDU_QUAL_SEQ = q.EDU_QUAL_SEQ"
                        + " LEFT JOIN MAS_EDU_DEGREES d ON q.EDU_DEGREE_SEQ = d.EDU_DEGREE_SEQ"
                        + " LEFT JOIN MAS_EDU_MAJORS m ON edu.EDU_MAJOR_SEQ = m.EDU_MAJOR_SEQ"
                        + " LEFT JOIN MAS_EDU_INSTITUTES i ON edu.EDU_INS_SEQ = i.EDU_INS_SEQ"
                    + " WHERE edu.RECORD_STATUS = @ActiveRecord"
                        + " AND edu.TMP_EMP_SEQ = @tmp_emp_seq";

                DataSet ds = new DataSet();
                DataTable dt;
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dt = ds.Tables[0].Copy();
                return dt;
            }

            catch (Exception ex)
            {
                return null;
            }

            finally
            {
                dbManager.Dispose();
            }
        }
        public DataTable SelectTmpEmployee(Nullable<int> TmpEmpSeq, Nullable<int> Status, Nullable<int> TypeEntry)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@TypeEntry", TypeEntry);
                dbManager.AddParameters(2, "@Status", Status);
                dbManager.AddParameters(3, "@TmpEmpSeq", TmpEmpSeq);

                string sql;
                sql = " SELECT emp.*"
                    + ", CONCAT(np.PREFIX_NAME,emp.FIRST_NAME,'  ',emp.LAST_NAME) AS FULL_NAME"
                    + " FROM HRM_TMP_EMPLOYEES emp"
                    + " LEFT JOIN MAS_NAME_PREFIXES np ON emp.NAME_PREFIX_SEQ = np.NAME_PREFIX_SEQ"
                    + " WHERE emp.RECORD_STATUS = @ActiveRecord"
                    + (Status.HasValue ? " AND emp.EMP_STATUS_SEQ = @Status" : "")
                    + (TypeEntry.HasValue ? " AND emp.ENT_TYPE_SEQ = @TypeEntry" : "")
                    + (TmpEmpSeq.HasValue ? " AND emp.TMP_EMP_SEQ = @TmpEmpSeq" : "");

                DataSet ds = new DataSet();
                DataTable dt;
                ds = dbManager.ExecuteDataSet(CommandType.Text, sql);
                dt = ds.Tables[0].Copy();
                return dt;
            }

            catch (Exception ex)
            {
                return null;
            }

            finally
            {
                dbManager.Dispose();
            }

        }
        public IList<PositionInfo> ListPositions(string EmployeeType)
        {
            IPositionDAL dal = DataAccess.CreatePositions();
            EmployeeTypeInfo etInfo = new EmployeeTypeInfo();
            PositionInfo posInfo = new PositionInfo();

            if (EmployeeType.Length > 0)
                etInfo.Sequence = Int32.Parse(EmployeeType);

            posInfo.EmployeeType = etInfo;
            return dal.ListPositions(posInfo);

        }
        public IList<PositionInfo> GetPositions(int PosSeq)
        {
            IPositionDAL dal = DataAccess.CreatePositions();
            //EmployeeTypeInfo etInfo = new EmployeeTypeInfo();
            //PositionInfo posInfo = new PositionInfo();

            //if (EmployeeType.Length > 0)
            //    etInfo.Sequence = Int32.Parse(EmployeeType);

            //posInfo.EmployeeType = etInfo;
            return dal.ListPositions(new PositionInfo(PosSeq));

        }
    }
}
